package house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import house.entity.Res;
import house.entity.User;
import house.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping(value = "/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/register")
    @ResponseBody
    public String register(@RequestBody String json, HttpServletResponse response) throws IOException {
        Res res = null;
        try {
            User user = JSONObject.parseObject(json, User.class);
            user.setRole("USER");
            res = userService.register(user);
        } catch (Exception e) {
            response.sendError(400, e.getMessage());
        }
        return JSON.toJSONString(res);
    }
}
